package com.mlethe.android.demo.seattable.presenter;

import androidx.fragment.app.FragmentActivity;

import com.mlethe.android.demo.R;
import com.mlethe.android.demo.seattable.base.BasePresenter;
import com.mlethe.android.demo.seattable.entity.Hall;
import com.mlethe.android.demo.seattable.entity.MovieInfo;
import com.mlethe.android.demo.seattable.entity.Obj;
import com.mlethe.android.demo.seattable.entity.Result;
import com.mlethe.android.demo.seattable.model.MovieModel;
import com.mlethe.android.demo.seattable.view.MovieView;
import com.mlethe.library.net.callback.Consumer;
import com.mlethe.library.utils.DateUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class MoviePresenter extends BasePresenter<MovieView> {

    private MovieModel movieModel;

    public MoviePresenter(FragmentActivity activity) {
        super(activity);
        movieModel = new MovieModel();
    }

    /**
     * 获取电影
     */
    public void getMovie() {
        HashMap<String, Object> map = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long nowTime = System.currentTimeMillis();
        map.put("start", sdf.format(new Date(nowTime)));
        map.put("end", sdf.format(new Date(nowTime + DateUtil.DAY * 2)));
        movieModel.cinemaPlays(map)
                .subscribe(new Consumer<Result<Obj<List<Hall>>>>() {

                    @Override
                    public void onSuccess(Result<Obj<List<Hall>>> objResult) {
                        if (!objResult.isOk()) {
                            getView().fail(objResult.getContent());
                        } else if (!objResult.getObject().getRes().isOk()) {
                            getView().fail(objResult.getObject().getRes().getErrorMessage());
                        } else {
                            getView().success(handlerHall(objResult.getObject().getRes().getData()));
                        }
                    }

                    @Override
                    public void onFailure(Throwable throwable) {
                        throwable.printStackTrace();
                        getView().fail(mFragmentActivity.getString(R.string.empty_network_error));
                    }
                });
    }

    /**
     * 处理播放计划表
     * @param result
     * @return
     */
    private List<MovieInfo> handlerHall(List<Hall> result) {
        List<MovieInfo> movies = new ArrayList<>();
        for (Hall hall : result) {
            boolean flag = false;
            MovieInfo movieInfo = hall.getMovieInfo().get(0);
//            Log.e("TAG", "handlerHall: " + hall.getBusinessDate() + "   name->" + movieInfo.getMovieName() + "    id->" + hall.getId() + "    hallId->" + hall.getHallId());
            for (MovieInfo movie : movies) {
                if (movieInfo.getCineMovieId().equals(movie.getCineMovieId())) {
                    flag = true;
                    String hallIds = movie.getHallIds() + ("," + hall.getId());
                    movie.setHallIds(hallIds);
                    break;
                }
            }
            if (!flag) {
                movieInfo.setHallIds(hall.getId());
                movieInfo.setTime(hall.getEndTime().getTime() - hall.getStartTime().getTime());
                movies.add(movieInfo);
            }
        }
        return movies;
    }
}
